import { defineStore } from 'pinia'
import { ref } from 'vue'
import { enrollmentApi } from '@/api'

export const useEnrollmentStore = defineStore('enrollment', () => {
  // 状态
  const enrollments = ref([])
  const total = ref(0)
  const loading = ref(false)
  const searchForm = ref({
    studentId: '',
    studentName: '',
    courseId: '',
    courseName: '',
    hasGrade: ''
  })
  const pagination = ref({
    page: 1,
    size: 10
  })

  // 获取选课记录列表
  const fetchEnrollments = async (params = {}) => {
    loading.value = true
    try {
      const queryParams = {
        ...searchForm.value,
        ...pagination.value,
        ...params
      }
      
      const response = await enrollmentApi.getEnrollments(queryParams)
      if (response.code === 200) {
        enrollments.value = response.data.records || []
        total.value = response.data.total || 0
        return true
      }
      return false
    } catch (error) {
      console.error('获取选课记录错误:', error)
      return false
    } finally {
      loading.value = false
    }
  }

  // 添加选课记录
  const createEnrollment = async (enrollmentData) => {
    try {
      const response = await enrollmentApi.enrollCourse(enrollmentData)
      if (response.code === 200) {
        await fetchEnrollments() // 刷新列表
        return { success: true, message: '选课成功' }
      }
      return { success: false, message: response.message || '选课失败' }
    } catch (error) {
      console.error('选课错误:', error)
      return { success: false, message: '网络错误，请稍后重试' }
    }
  }

  // 更新成绩
  const updateGrade = async (enrollmentId, grade) => {
    try {
      const response = await enrollmentApi.updateGrade({ sno: enrollmentId.sno, cno: enrollmentId.cno, grade })
      if (response.code === 200) {
        await fetchEnrollments() // 刷新列表
        return { success: true, message: '成绩录入成功' }
      }
      return { success: false, message: response.message || '成绩录入失败' }
    } catch (error) {
      console.error('成绩录入错误:', error)
      return { success: false, message: '网络错误，请稍后重试' }
    }
  }

  // 批量更新成绩
  const batchUpdateGrade = async (gradeData) => {
    try {
      const response = await enrollmentApi.batchUpdateGrades(gradeData)
      if (response.code === 200) {
        await fetchEnrollments() // 刷新列表
        return { success: true, message: '批量录入成绩成功' }
      }
      return { success: false, message: response.message || '批量录入成绩失败' }
    } catch (error) {
      console.error('批量录入成绩错误:', error)
      return { success: false, message: '网络错误，请稍后重试' }
    }
  }

  // 删除选课记录（退课）
  const removeEnrollment = async (enrollmentId) => {
    try {
      const response = await enrollmentApi.dropCourse(enrollmentId.sno, enrollmentId.cno)
      if (response.code === 200) {
        await fetchEnrollments() // 刷新列表
        return { success: true, message: '退课成功' }
      }
      return { success: false, message: response.message || '退课失败' }
    } catch (error) {
      console.error('退课错误:', error)
      return { success: false, message: '网络错误，请稍后重试' }
    }
  }

  // 重置搜索条件
  const resetSearch = () => {
    searchForm.value = {
      studentId: '',
      studentName: '',
      courseId: '',
      courseName: '',
      hasGrade: ''
    }
    pagination.value.page = 1
  }

  // 设置分页
  const setPagination = (page, size) => {
    pagination.value.page = page
    pagination.value.size = size
  }

  return {
    // 状态
    enrollments,
    total,
    loading,
    searchForm,
    pagination,
    
    // 方法
    fetchEnrollments,
    createEnrollment,
    updateGrade,
    batchUpdateGrade,
    removeEnrollment,
    resetSearch,
    setPagination
  }
})